package com.example.music.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.music.mapper.LovemusicMapper;
import com.example.music.model.Lovemusic;
import com.example.music.model.Music;
import com.example.music.model.User;
import com.example.music.service.LovemusicService;
import com.example.music.service.MusicService;
import com.example.music.util.Constant;
import com.example.music.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: LoveMusicController
 * Package: com.example.music.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-04-15
 * Time: 18:21
 */

@RestController
@RequestMapping("/loveMusic")
public class LoveMusicController {

    @Autowired
    private MusicService musicService;

    @Autowired
    private LovemusicService lovemusicService;

    @Autowired
    private LovemusicMapper lovemusicMapper;

    //收藏自己喜欢的音乐
    @PostMapping("/likeMusic")
    public Result<Boolean> likeMusic(@RequestParam("music_id") Integer music_id, HttpServletRequest request){
        //请求{post,/lovemusic/likeMusic,data:用户id//音乐id}
        //响应{"status":0,"message":"点赞成功","data":ture}

        //1.从Session中获取用户ID
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);

        //根据用户Id 和 歌曲Id 在LoveMusic表中查询是否存在两者都相等的时候 存在那么就已经收藏过了
        QueryWrapper<Lovemusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        queryWrapper.eq("music_id",music_id);

        Lovemusic ret = lovemusicService.getOne(queryWrapper);
        if (ret == null){
            //说明 没有收藏
            //向表中添加一条数据
            Lovemusic lovemusic = new Lovemusic();
            lovemusic.setUserId(user.getId());
            lovemusic.setMusicId(music_id);
            boolean sut = lovemusicService.save(lovemusic);
            if (sut){
                return new Result<>(1,"收藏成功",true);
            }
        }
        //表示已经收藏
        return new Result<>(-1,"已经收藏了该歌曲",false);
    }

    //查询自己喜欢的音乐
    @PostMapping("/like")
    public Result<List<Music>> like(HttpServletRequest request,String name) {
        //根据Session获取用户ID

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        return null;
    }
}
